const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    index2: 0,
    bumen: ['请选择部门'],
    zhaolujiguan: ['请选择招录机关'],
    theList: '',
    page: 27
  },
  bindPickerChange: function (e) {
    let num = e.detail.value;
    let _ = this;
    console.log('bumen发送选择改变，携带值为', _.data.bumen[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=44524&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'bumen',
        grtext: _.data.bumen[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择招录机关')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].zhaolujiguan)
        }
        _.setData({
          zhaolujiguan: list
        });
      }
    })
  },
  bindPickerChange2: function (e) {
    let num2 = e.detail.value;
    let _ = this;
    console.log('zhaolujiguan发送选择改变，携带值为', _.data.zhaolujiguan[num2]);
    this.setData({
      index2: e.detail.value
    })

  },

  onLoad: function (options) {
    var _ = this;
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=44524&callback=?', //接收url
      data: {
        level: '1',
        bumen: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择部门')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].bumen)
        }
        _.setData({
          bumen: list
        });
      }
    })
    var phone = wx.getStorageSync('mssphone')
    if (phone) {
      _.setData({
        phone: phone
      })
    }
    console.log(options)
    wx.setStorageSync('area', options.area)
    wx.setStorageSync('agent', options.agent)
    _.setData({
      area: options.area,
      uid: options.agent
    })
    if (options.page) {
      _.setData({
        page: options.page
      })
    }
  },
  getPhoneNumber(e) {
    var _ = this;

    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      _.setData({
        mobile: res.result,
      })
      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 36,
          mobile: res.result,
          areaCode: _.data.area,
          agent: _.data.uid,
          pageId: _.data.page
        },
        success: res => {
          console.log(res);
        }
      })
      wx.setStorageSync('mssphone', res.result)
      console.log(res.result)
      wx.request({ //发送请求
        url: "https://zg99.offcn.com/index/chaxun/register/?actid=44524&callback=?",
        data: {
          phone: res.result,
          area: _.data.area,
          uid: _.data.uid
        }, //接收参数
        success(res) { //返回
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          };
          console.log(theJson)

          var bumen = _.data.bumen[_.data.index];
          var zhaolujiguan = _.data.zhaolujiguan[_.data.index2];
          wx.request({ //发送请求
            url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=44524&callback=?",
            data: {
              bumen: bumen,
              zhaolujiguan: zhaolujiguan,
              limits: '100'
            }, //接收参数
            success(res) { //返回
              var jsonStr = res.data;
              jsonStr = jsonStr.replace("(", "");
              jsonStr = jsonStr.replace(")", "");
              if (typeof jsonStr != 'object') {
                jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                var theJson = JSON.parse(jsonStr);
              };
              console.log(theJson)
              _.setData({
                theList: theJson.lists
              })
              console.log(_.data.theList)
              wx.pageScrollTo({
                scrollTop: 500,
                duration: 300
              })
            }
          })


      
        }
      })

    }).catch(err => {
      console.error(err);
    });



  },


  tijiao: function (res) {
    var _ = this;
    var bumen = _.data.bumen[_.data.index];
    var zhaolujiguan = _.data.zhaolujiguan[_.data.index2];
    wx.request({ //发送请求
      url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=44524&callback=?",
      data: {
        bumen: bumen,
        zhaolujiguan: zhaolujiguan,
        limits: '100'
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        };
        console.log(theJson)
        _.setData({
          theList: theJson.lists
        })
        console.log(_.data.theList)
        wx.pageScrollTo({
          scrollTop: 500,
          duration: 300
        })
      }
    })




  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})